package com.inoneone.toolkit.exception;

/**
 *
 * ClassName: BusinessRuntimeException <br/>
 * Function: 统一异常类. <br/>
 * date: 2016年8月11日 上午11:10:45 <br/>
 *
 * @author 姬远玄
 * @version
 * @since JDK 1.7
 */
public class BusinessRuntimeException extends RuntimeException {

    /**
     * serialVersionUID:TODO(用一句话描述这个变量表示什么).
     *
     * @since JDK 1.7
     */
    private static final long serialVersionUID = -1097622340823098502L;

    /**
     * 错误码
     */
    private String errCode;

    /**
     * 错误描述
     */
    private String errMsg;

    /**
     * 扩展信息,可以添加动态的信息
     */
    private String attach;

    /**
     * 默认构造函数，不加hessian反序列化会出错
     */
    public BusinessRuntimeException() {
    }

    public BusinessRuntimeException(String errCode, String errMsg) {
        super(String.format("[%s]%s", errCode, errMsg));
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public BusinessRuntimeException(String errCode, String errMsg, String attach) {
        super(String.format("[%s]%s,%s", errCode, errMsg, attach));
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.attach = attach;
    }

    public BusinessRuntimeException(String errCode, String message, Throwable cause) {
        super(String.format("[%s]%s", errCode, message), cause);
        this.errCode = errCode;
        this.errMsg = message;
    }

    public BusinessRuntimeException(String errCode, String message, Throwable cause, String attach) {
        super(String.format("[%s]%s,%s", errCode, message, attach), cause);
        this.errCode = errCode;
        this.errMsg = message;
        this.attach = attach;
    }

    public BusinessRuntimeException(ExceptionModel exceptionModel)
    {
        super(String.format("[%s]%s", exceptionModel.getErrCode(), exceptionModel.getErrMsg()));
        this.errCode = exceptionModel.getErrCode();
        this.errMsg = exceptionModel.getErrMsg();
    }

    public BusinessRuntimeException(ExceptionModel exceptionModel, String attach)
    {
        super(String.format("[%s]%s,%s", exceptionModel.getErrCode(), exceptionModel.getErrMsg(), attach));
        this.errCode = exceptionModel.getErrCode();
        this.errMsg = exceptionModel.getErrMsg();
        this.attach = attach;
    }

    public BusinessRuntimeException(ExceptionModel exceptionModel, Throwable cause)
    {
        super(String.format("[%s]%s", exceptionModel.getErrCode(), exceptionModel.getErrMsg()));
        this.errCode = exceptionModel.getErrCode();
        this.errMsg = exceptionModel.getErrMsg();
    }

    public BusinessRuntimeException(ExceptionModel exceptionModel, Throwable cause, String attach)
    {
        super(String.format("[%s]%s,%s", exceptionModel.getErrCode(), exceptionModel.getErrMsg(), attach));
        this.errCode = exceptionModel.getErrCode();
        this.errMsg = exceptionModel.getErrMsg();
        this.attach = attach;
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public String getAttach() {
        return attach;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BusinessRuntimeException other = (BusinessRuntimeException) obj;
        if (attach == null) {
            if (other.attach != null)
                return false;
        } else if (!attach.equals(other.attach))
            return false;
        if (errCode == null) {
            if (other.errCode != null)
                return false;
        } else if (!errCode.equals(other.errCode))
            return false;
        if (errMsg == null) {
            if (other.errMsg != null)
                return false;
        } else if (!errMsg.equals(other.errMsg))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((attach == null) ? 0 : attach.hashCode());
        result = prime * result + ((errCode == null) ? 0 : errCode.hashCode());
        result = prime * result + ((errMsg == null) ? 0 : errMsg.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "BusinessRuntimeException [errCode=" + errCode + ", errMsg=" + errMsg + ", attach=" + attach + "]";
    }
}
